
#include "stm8l15x.h"
 
#include "PublicSet1.h"
 
#include "extern1.h"
#include "backdata.h"


void	Reset_LVSave(void){ //0
	u8 n;
	u8 a;
	u8 fl[sizeof(strFeiLvSave)*_MAX_TARRIF_];

	a = RamData.LVSave.FeilvNum;

	ClrRam(fl, sizeof(strFeiLvSave)*_MAX_TARRIF_);
	ClrRam( (u8*)&RamData.LVSave, sizeof(RamData.LVSave));
	RamData.LVSave.FeilvNum = a;
	RamData.LVSave.End = 0x16;
	RamData.LVSave.cs = GetSum((u8*)&RamData.LVSave,    sizeof(RamData.LVSave) - 3);
	wrEEP_ST8((u8*)&RamData.LVSave, (u8*)&eepLvSave.dq, sizeof(RamData.LVSave));
	wrEEP_ST8(fl, (u8*)&eepLvSave.fl, sizeof(strFeiLvSave)*_MAX_TARRIF_);
	wrEEP_ST8((u8*)&RamData.LVSave, (u8*)&eepLvSave.dqBck, sizeof(RamData.LVSave));
	wrEEP_ST8((u8*)&RamData.LVSave, (u8*)&eepLvSave.dqBck, sizeof(RamData.LVSave));
	wrEEP_ST8(fl, (u8*)&eepLvSave.flBck, sizeof(strFeiLvSave)*_MAX_TARRIF_);
	Up_BkLVSave();
}


void	Up_BkLVSave(void){//0
	u8	Cnt;
	
	for(Cnt = 0; Cnt < sizeof(strLVSave); Cnt++){
		((u8*)&BkLVSave[2])[Cnt] = 0xFF - ((u8*)&RamData.LVSave)[Cnt];
	}
	CopyRam( (u8*)&BkLVSave[0], (u8*)&RamData.LVSave, sizeof(strLVSave));
	CopyRam( (u8*)&BkLVSave[1], (u8*)&BkLVSave[2], sizeof(strLVSave));
}

void	Check_LVSave(void){//0
	u8	Cnt;
	u8	*pLV1, *pLV2;
	
	if((BkLVSave[0].End != 0x16) || (RamData.LVSave.End != 0x16)){
		Up_BkLVSave();
		return;
	}
	pLV1 = (u8*)&BkLVSave[0];
	pLV2 = (u8*)&BkLVSave[1];
	for(Cnt = 0; Cnt < sizeof(strLVSave); Cnt++){
		if((pLV1[Cnt]+pLV2[Cnt]) != 0xFF){
			Up_BkLVSave();
			return;
		}
	}
	CopyRam( (u8*)&RamData.LVSave, (u8*)&BkLVSave, sizeof(strLVSave));
}


void	Read_LV_2(u8* s){ //0
	u16	Length;
	return ; // opt 
	Length = *(u16*)(s);
	if( (Length&0x00FF) != _MAX_TARRIF_ )
		return;
	if(Length){
		RamData.WriterCode = *(u32*)(s+2);
	}
}

void	Write_LV_2(){ //0
	u8	Buf[2+4];
	return ; // opt 
	u16p(Buf) = 0x0100 + _MAX_TARRIF_;
	u32p(Buf+2) = RamData.RTCTime;
	wrEEP_ST8(Buf, (u8*)&eepLvSave.lv2, 6);
}

void	Read_ELVSave(){//0
	u16 cs1,cs2;

	cs1 = GetSum((u8*)&eepLvSave.dq,    sizeof(RamData.LVSave) - 3);
	cs2 = GetSum((u8*)&eepLvSave.dqBck, sizeof(RamData.LVSave) - 3);

	if(cs1 == eepLvSave.dq.cs){
		CopyRam((u8*)&(RamData.LVSave), (u8*)&eepLvSave.dq, sizeof(strLVSave) );
	}else if(cs2 == eepLvSave.dqBck.cs){
		CopyRam((u8*)&(RamData.LVSave), (u8*)&eepLvSave.dqBck, sizeof(strLVSave));
	}else{
		Reset_LVSave();
	}
	Read_LV_2(eepLvSave.lv2);
}

void	Write_FLSave(){//0
	if(strCmp((u8*)&RamData.LVSave, (u8*)&eepLvSave.dq, sizeof(strLVSave)) == 0){
		return;
	}
	RamData.LVSave.cs = GetSum((u8*)(&RamData.LVSave), sizeof(RamData.LVSave) - 3);
	RamData.LVSave.End = 0x16;
	wrEEP_ST8((u8*)&RamData.LVSave,     (u8*)&eepLvSave.dq,      sizeof(strLVSave));
	wrEEP_ST8((u8*)&RamData.LVSave,     (u8*)&eepLvSave.dqBck,   sizeof(strLVSave));
}

void	wrFeiLvToEEP(void){//0
	strFeiLvSave s;
	
	CopyRam((u8*)&s, (u8*)&RamData.LVSave.FLUse, sizeof(strFLUse));
	s.cs = GetSum((u8*)&s, sizeof(strFLUse));
	wrEEP_ST8((u8*)&s, (u8*)&eepLvSave.fl[RamData.LVSave.FeilvNum], sizeof(strFeiLvSave));
	wrEEP_ST8((u8*)&s, (u8*)&eepLvSave.flBck[RamData.LVSave.FeilvNum], sizeof(strFeiLvSave));

}

void	Up_FLSave(void){//0
	Write_FLSave();
}

void	Up_LVSave(){//0
	Write_FLSave();
	Write_LV_2();
}


